import { Observer } from "./Observer";

/**
 * 这个类是作为美女和两位男士之间添加一个中间人
 */
class 第三方中心管理_老姨妈 {
    subscribers: Map<String, Array<Function>>;

    constructor() {
        this.subscribers = new Map()
    }
    subscribe(type: string, fn: Function) {
        if (!this.subscribers.get(type)) {
            this.subscribers.set(type, [])
        }
        this.subscribers.get(type).push(fn);
    }
    unsubscribe(type: string, fn?: Function) {
        let listeners = this.subscribers.get(type);
        if (!listeners || !listeners.length) return;
        if (!fn) { this.subscribers.delete(type); return; }
        this.subscribers.set(type, listeners.filter(v => v !== fn));
    }
    publish(type: string, ...args: any[]) {
        let listeners = this.subscribers.get(type);
        if (!listeners || !listeners.length) return;
        listeners.forEach(fn => fn(...args));
    }
}


export let 阿姨 = new 第三方中心管理_老姨妈();